/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.vijay.mvnbuilddownload;

import java.io.IOException;
import java.io.InputStream;
//import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author elangovanv
 */
public class XMLUtil {

    private Document parseXML(InputStream is) throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        
        return builder.parse(is);
    }

    public Document parseXML(String urlLocation, String filename) throws SAXException, IOException, ParserConfigurationException {

        URL url=new URL(urlLocation+"/"+filename);
        URLConnection urlConnection =  url.openConnection();
       
        HttpURLConnection connection;

        if (urlLocation.contains("https")) {
            connection = (HttpsURLConnection)urlConnection;
        } else {
            connection = (HttpURLConnection)urlConnection;
        } 
        connection.setRequestMethod("GET");
        connection.setInstanceFollowRedirects(true);
        connection.connect();

        return parseXML(connection.getInputStream());
    }


    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
        XMLUtil xml = new XMLUtil();
        Document ldocument = xml.parseXML("http://repository.dnbgp.dnb.com/nexus/content/repositories/inhouse.snapshots/com/dnb/dnbi/creditCommon/dnbi-creditCommon-assembly/9.0.8-SNAPSHOT/", "maven-metadata.xml");
        MetadataParser parser=MetadataParser.getMetadataParser(ldocument);
        parser.parse();
        //System.out.println(parser.toString());
    }
}
